import Vue from 'vue';
import VueRouter from 'vue-router';
import iView from 'view-design';

import util from '@/libs/util'

import Setting from '@/setting';

import store from '@/store/index';

// 路由数据
import routes from './routes';

Vue.use(VueRouter);

// 导出路由 在 main.js 里使用
const router = new VueRouter({
    routes,
    mode: Setting.routerMode,
    base: Setting.routerBase
});

/**
 * 路由拦截
 * 权限验证
 */
function filterAddress (addressF, address) {
    addressF.forEach((ee) => {
        if (ee.children && ee.children.length) {
            filterAddress(ee.children, address);
        } else {
            address.push(ee.path);
        }
    });
}
function redirectUrl (title) {
    var address = [];
    let headerId = util.storage.get('menus').header.find(
        (ee) => ee.title === title
    );
    if (headerId) {
        let headerIds = headerId.name;
        let menus = util.storage.get('menus').sider.filter(
            (ee) => ee.header === headerIds
        )[0];
        filterAddress([menus], address);
        return address;
    } else {
        return [];
    }
}
// router.beforeEach((to, from, next) => {
//     if (Setting.showProgressBar) iView.LoadingBar.start();

//     // 判断是否需要登录才可以进入
//     if (to.matched.some(_ => _.meta.auth)) {
//         // 这里依据 token 判断是否登录，可视情况修改
//         const token = util.cookies.get('token');
//         if (token && token !== 'undefined') {
//             let sider = util.storage.get('menus').sider
//             console.log(sider, 'sider')
//             let header = util.storage.get('menus').header
//             console.log(header, 'header')
//             if (to.path === '/httpsetting') {
//                 let paths = sider.find(e => e.path === '/httpsetting')
//                 if (paths) next();
//             } else if (to.path === '/setting') {
//                 let paths = sider.find(e => e.path === '/home')
//                 if (paths) next();
//             } else {
//                 next();
//             }
//         } else {
//             // 没有登录的时候跳转到登录界面
//             // 携带上登陆成功之后需要跳转的页面完整路径
//             next({
//                 name: 'login',
//                 query: {
//                     redirect: to.fullPath
//                 }
//             });
//         }
//     } else {
//         // 不需要身份校验 直接通过
//         next();
//     }
// });
router.beforeEach((to, from, next) => {
    if (Setting.showProgressBar) iView.LoadingBar.start();
    // 判断是否需要登录才可以进入
    if (to.matched.some((_) => _.meta.auth)) {
        // 这里依据 token 判断是否登录，可视情况修改
        let headername = ''
        const token = util.cookies.get('token');
        if (token && token !== 'undefined') {
            let menus = util.storage.get('menus')
            if (to.path == '/yewu') {
                util.storage.set('home', false)
                menus.header.forEach(it => {
                    if (it.path == to.path) {
                        headername = it.name
                    }
                })
                let sider = menus.sider
                let paths = []
                sider.forEach(item => {
                    if (item.header === headername) {
                        paths.push(item.path)
                    }
                    
                })
                next({
                        path: paths[0]
                    });
                // console.log(paths, 'paths');
            } else if (to.path == '/setting') {
                util.storage.set('home', false)
                // 取headername  menus.header
                // 取 menus.sider  过滤 sider
                // 路由走sider第一个
                menus.header.forEach(it => {
                    if (it.path == to.path) {
                        headername = it.name
                    }
                })
                let sider = menus.sider
                let paths = []
                sider.forEach(item => {
                    if (item.header === headername) {
                        paths.push(item.path)
                    }
                    next({
                        path: paths[0]
                    });
                    // console.log(paths, 'paths');
                })
            } else if (to.path == '/report') {
                util.storage.set('home', false)
                // 取headername  menus.header
                // 取 menus.sider  过滤 sider
                // 路由走sider第一个
                menus.header.forEach(it => {
                    if (it.path == to.path) {
                        headername = it.name
                    }
                })
                let sider = menus.sider
                let paths = []
                sider.forEach(item => {
                    if (item.header === headername) {
                        paths.push(item.path)
                    }
                    next({
                        path: paths[0]
                    });
                    // console.log(paths, 'paths');
                })
            } else if (to.path == '/release') {
                util.storage.set('home', false)
                // 取headername  menus.header
                // 取 menus.sider  过滤 sider
                // 路由走sider第一个
                menus.header.forEach(it => {
                    if (it.path == to.path) {
                        headername = it.name
                    }
                })
                let sider = menus.sider
                let paths = []
                sider.forEach(item => {
                    if (item.header === headername) {
                        paths.push(item.path)
                    }
                    // next({
                    //     path: paths[0]
                    // });
                })
                if (paths.length) {
                    next({
                        path: paths[0]
                    });
                }
            }else if (to.path == '/public') {
                util.storage.set('home', false)
                // 取headername  menus.header
                // 取 menus.sider  过滤 sider
                // 路由走sider第一个
                menus.header.forEach(it => {
                    if (it.path == to.path) {
                        headername = it.name
                    }
                })
                let sider = menus.sider
                let paths = []
                sider.forEach(item => {
                    if (item.header === headername) {
                        paths.push(item.path)
                    }
                    // next({
                    //     path: paths[0]
                    // });
                })
                if (paths.length) {
                    next({
                        path: paths[0]
                    });
                }
            }
             else if (to.path == '/home') {
                util.storage.set('home', true)
                menus.header.forEach(it => {
                    if (it.path == to.path) {
                        headername = it.name
                        // next({
                        //     path: '/home'
                        // });
                    }
                })
                let sider = menus.sider
                let paths = []
                sider.forEach(item => {
                    if (item.header === headername) {
                        paths.push(item.path)
                    } else {
                        next();
                    }
                    // next({
                    //     path: paths[0]
                    // });
                    // console.log(paths, 'paths');
                })
            } else {
                next();
            }
        } else {
            // 没有登录的时候跳转到登录界面
            // 携带上登陆成功之后需要跳转的页面完整路径
            next({
                name: 'login',
                query: {
                    redirect: to.fullPath
                }
            });
        }
    } else {
        // 不需要身份校验 直接通过
        next();
        // next({
        //     path: to.path
        // });
    }
});
router.afterEach(to => {
    if (to.path == '/yewu' || to.path == '/setting' || to.path == '/home' || to.path == '/report' || to.path == '/release'|| to.path == '/public') {

    } else {
        if (Setting.showProgressBar) iView.LoadingBar.finish();
        // 多页控制 打开新的页面
        store.dispatch('admin/page/open', to);
        // 更改标题
        util.title({
            title: to.meta.title
        });
        // 返回页面顶端
        window.scrollTo(0, 0);
    }
});

export default router;
